package me.project.jobster.service;

import me.project.jobster.dao.mybatis.mapper.FriendMapper;
import me.project.jobster.dao.mybatis.model.Company;
import me.project.jobster.dao.mybatis.model.Friend;
import me.project.jobster.dao.mybatis.model.Student;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author mbryce
 * @date 2018/4/28
 */
@Service
public class FriendService {

    @Autowired
    private FriendMapper friendMapper;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * check friendship
     * @param userId
     * @param friendId
     * @return
     */
    public Boolean isFriend(String userId, String friendId){
        Friend friend = new Friend();
        friend.setsId(userId);
        friend.setfId(friendId);
        int count = friendMapper.selectCount(friend);
        return count > 0;
    }

    /**
     * save the relationship
     * @param userId
     * @param userName
     * @param friendId
     * @param friendName
     */
    public void beFriends(String userId, String userName,
                         String friendId, String friendName){
        Friend friend = new Friend();
        friend.setsId(userId);
        friend.setfId(friendId);
        friend.setfName(friendName);;
        friendMapper.insert(friend);

        //save both
        Friend friend2 = new Friend();
        friend2.setsId(friendId);
        friend2.setfId(userId);
        friend2.setfName(userName);
        friendMapper.insert(friend2);
    }

    /**
     * get all friend by userId
     * @param userId
     */
    public List<Student>  getAllFriend(String userId){
        String sql = "select a.* from student a where a.s_id in(select b.f_id from friend b where b.s_id = '"+userId+"' )";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Student.class));
    }
}
